<template>
  <div class="login-container">
    <div class="overlay"></div>
    <div class="login-box">
      <h2 class="login-title">{{ defaultSettings.title }}</h2>

      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号" size="large">
            <template #prefix>
              <svg-icon name="user" class="input-icon" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" show-password type="password" auto-complete="off" placeholder="请输入密码"
            size="large" @keyup.enter="handleLogin">
            <template #prefix>
              <svg-icon name="password" class="input-icon" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item style="margin-top: 30px;">
          <el-button type="primary" :loading="loading" size="large" style="width: 100%; border-radius: 24px"
            @click.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer" v-html="defaultSettings.copyright"></div>
    </div>
  </div>
</template>

<script setup name="login">
import Cookies from 'js-cookie'
import defaultSettings from '@/settings'
import useUserStore from '@/store/modules/user'
import { ref, reactive, getCurrentInstance } from 'vue'
import { setToken } from '@/utils/auth'
import { useRouter } from 'vue-router'

const loginRef = ref(null)
const userStore = useUserStore()
const router = useRouter()
const { proxy } = getCurrentInstance()

const loginForm = ref({
  username: 'admin',
  password: '123456'
})

const localUsers = reactive([
  { username: "admin", password: "123456" },
  { username: "test", password: "test1234" },
  { username: "guest", password: "guest8888" }
])

const loginRules = {
  username: [{ required: true, message: '请输入您的账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入您的密码', trigger: 'blur' }]
}

const loading = ref(false)

function handleLogin() {
  loginRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      const matchedUser = localUsers.find(
        (user) =>
          user.username === loginForm.value.username &&
          user.password === loginForm.value.password
      )
      if (!matchedUser) {
        loading.value = false
        proxy.$modal?.msgError?.('账号或密码错误') 
        return
      }
      setToken('mock-token')
      userStore.setToken('mock-token')
      userStore.setUserInfo({ username: matchedUser.username })
      router.push({ path: '/' })
    }
  })
}

function getCookie() {
  const username = Cookies.get('username')
  const password = Cookies.get('password')
  loginForm.value.username = username ?? ''
  loginForm.value.password = password ?? ''
}
getCookie()
</script>

<style lang="scss" scoped>
.login-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  justify-content: flex-end; // 使内部元素靠右排列
  align-items: center;
  background-image: url('@/assets/images/bg.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-blend-mode: multiply;
  /* 混合模式 */
}

.login-box {
  background: rgba(50, 120, 200, 0.7);
  /* 新背景色 */
  padding: 40px 50px 30px 50px;
  border-radius: 16px;
  width: 400px;
  box-shadow: 0 0 15px rgba(50, 120, 200, 0.5);
  /* 新阴影 */
  border: 1px solid #3498db;
  /* 添加边框 */
  margin-right: 450px; // 与右侧边缘留出间距
}

.login-title {
  text-align: center;
  font-size: 26px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #fff;
  text-shadow: 0 0 5px #3498db;
  /* 新文字阴影 */
  font-family: 'Montserrat', sans-serif;
  /* 新字体 */
}

.login-form .el-input__inner {
  background-color: rgba(255, 255, 255, 0.1) !important;
  /* 新背景色 */
  color: #fff;
  border: 1px solid #3498db;
  /* 新边框 */
  border-radius: 8px;
  padding-left: 38px;
  transition: border - color 0.3s ease;
  /* 过渡效果 */
}

.login-form .el-input__inner:focus {
  border-color: #2980b9;
  /* 聚焦时边框颜色 */
}

.login-form .el-input__prefix {
  color: #bdc3c7;
  left: 8px;
}

.el-button {
  border-radius: 24px;
  background-image: linear-gradient(to right, #3498db, #2980b9);
  /* 新渐变 */
  color: #fff;
  font-weight: bold;
  letter-spacing: 1px;
  transition: all 0.3s ease;
  /* 过渡效果 */
}

.el-button:hover {
  background-image: linear-gradient(to right, #2980b9, #2471a3);
  box-shadow: 0 0 10px #3498db;
  /* 悬停发光效果 */
}

.login-footer {
  text-align: center;
  font-size: 12px;
  color: #bdc3c7;
  /* 新颜色 */
  margin-top: 20px;
}
</style>